
package com.application.chiettinhdienluc.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.application.chiettinhdienluc.model.LoaiHoSoModel;

public class LoaiHoSoDataSource {
    // Database fields
    private final MySQLiteHelper dbHelper;

    public static final String TABLE_LOAIHOSO = "D_LOAIHOSO";
    public static final String COLUMN_MA_LOAIHS = "MA_LOAIHS";
    public static final String COLUMN_MO_TA = "MO_TA";
    public static final String COLUMN_NHOM = "NHOM";

    private final static String TAG = LoaiHoSoDataSource.class.getSimpleName();

    public LoaiHoSoDataSource() {
        dbHelper = MySQLiteHelper.getInstance();
    }

    public long insert(LoaiHoSoModel loaihoso) {

        long row_id = -1;
        
        try {
        	if(dbHelper != null && dbHelper.getDb() != null)
        	{
        		ContentValues cv = new ContentValues();

                cv.put(COLUMN_MA_LOAIHS, loaihoso.getMaLoaiHoSo());
                cv.put(COLUMN_MO_TA, loaihoso.getMoTa());
                cv.put(COLUMN_NHOM, loaihoso.getNhom());

                row_id = dbHelper.getDb().insert(TABLE_LOAIHOSO, null, cv);
        	}
		} catch (Exception e) {
		}
        finally{
		}

        return row_id;
    }

    public int update(LoaiHoSoModel loaihoso) {

    	int numRowEffect = 0;
    	try {
    		if(dbHelper != null && dbHelper.getDb() != null)
    		{
    			ContentValues cv = new ContentValues();

                cv.put(COLUMN_MA_LOAIHS, loaihoso.getMaLoaiHoSo());
                cv.put(COLUMN_MO_TA, loaihoso.getMoTa());
                cv.put(COLUMN_NHOM, loaihoso.getNhom());

                String whereClause = "MA_LOAIHS" + "=?" + " and " + "MO_TA" + "=?" + " and " + "NHOM"
                        + "=?";
                String[] whereArgs = new String[] {
                        loaihoso.getMaLoaiHoSo(), loaihoso.getMoTa(), loaihoso.getNhom()
                };

                numRowEffect = dbHelper.getDb().update(TABLE_LOAIHOSO, cv, whereClause, whereArgs);
    		}
		} catch (Exception e) {
			// TODO: handle exception
		}
    	 finally{
 		}

        return numRowEffect;
    }
    
    public ArrayList<LoaiHoSoModel> loadLoaiHoSo() {
		ArrayList<LoaiHoSoModel> listLoaiHoSoModel = new ArrayList<LoaiHoSoModel>();
		Cursor c = null;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + LoaiHoSoModel.LOAIHOSO_TABLE_NAME, null);

				if (c != null && c.getCount() > 0) {
					int colId_MaLoaiHS = c.getColumnIndex(LoaiHoSoModel.LOAIHOSO_FULL_PROJECTION[0]);
					int colId_MoTa= c.getColumnIndex(LoaiHoSoModel.LOAIHOSO_FULL_PROJECTION[1]);
					int colId_Nhom = c.getColumnIndex(LoaiHoSoModel.LOAIHOSO_FULL_PROJECTION[2]);			

					c.moveToFirst();
					do {
						try {
							String maLoaiHS = c.getString(colId_MaLoaiHS);
							String moTa = c.getString(colId_MoTa);
							String nhom = c.getString(colId_Nhom);					

							LoaiHoSoModel lhs = new LoaiHoSoModel();

							lhs.setMaLoaiHoSo(maLoaiHS);
							lhs.setMoTa(moTa);
							lhs.setNhom(nhom);					

							listLoaiHoSoModel.add(lhs);
						} catch (Exception e) {
						}
					} while (c.moveToNext());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return listLoaiHoSoModel;
	}
}
